{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ec413e6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os, time, datetime\n",
    "\n",
    "import requests\n",
    "import time\n",
    "import json\n",
    "import random\n",
    "\n",
    "os.chdir(\"/Users/xiaosongw/Dropbox/Research/InformedSources/Replication/Build\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93a0d4e1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df_od = pd.read_csv(\"./Output/od_long_mel.csv\")\n",
    "print(df_od.shape)\n",
    "with open(\"./Output/tzn2coor_mel.txt\", \"r\") as f:\n",
    "    data=f.read()\n",
    "d_tzn2coor={float(k):v for k, v in json.loads(data).items()} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "802e2889",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df_in = df_od[df_od['tzn0']!=df_od['tzn1']].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa01285a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "start = time.time()\n",
    "l_in=df_in['tzn0'].unique().tolist()\n",
    "ii=0\n",
    "for i in l_in:\n",
    "    l_dest_tzn=df_in.loc[df_in['tzn0']==i, 'tzn1'].tolist()\n",
    "    l_dest_coor=df_in.loc[df_in['tzn0']==i, 'tzn1'].map(d_tzn2coor).tolist()\n",
    "    n_=len(l_dest_tzn)//100\n",
    "    l_dist_out=[]\n",
    "    l_dura_out=[]\n",
    "    l_dest_out=[]\n",
    "    str_orig=d_tzn2coor[i]\n",
    "    for j in range(0,n_+1):\n",
    "        str_dest=';'.join(l_dest_coor[j*100:(j+1)*100])\n",
    "        url='http://router.project-osrm.org/table/v1/driving/{};{}?sources=0&annotations=distance,duration'.format(\n",
    "            str_orig, str_dest)\n",
    "        try:\n",
    "            out=requests.get(url).content\n",
    "            jout=json.loads(out)\n",
    "            l_dist_out=l_dist_out+jout['distances'][0][1:]\n",
    "            l_dura_out=l_dura_out+jout['durations'][0][1:]\n",
    "            l_dest_out=l_dest_out+l_dest_tzn[j*100:(j+1)*100]\n",
    "        except: \n",
    "            print('error {}'.format(i))\n",
    "            continue\n",
    "    df_out=pd.DataFrame({'tzn1':l_dest_out, 'distance':l_dist_out, 'duration':l_dura_out})\n",
    "    df_out['tzn0']=i\n",
    "    ii+=len(df_out)\n",
    "    df_out.to_csv(\"./Temp/tz2tz_o{}.csv\".format(i), index=False)\n",
    "    with open('log.txt', 'a') as f:\n",
    "        f.write('tzn {} done! collected {}. Time {} min'.format(i, ii, round((time.time()-start)/60, 1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fe35164",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "df = pd.DataFrame()\n",
    "l_files = [i for i in os.listdir(\"./Temp/\") if 'tz2tz_o' in i]\n",
    "for i in l_files:\n",
    "    df = pd.concat([df, pd.read_csv(\"./Temp/\"+i)], axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37f9e5c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.sort_values(['tzn0', 'tzn1'], ignore_index=True).to_csv(\"./Output/tz2tz_out.csv\", index=False)"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
